/**
 * 
 */
package com.local.fastJson.fastjsonserial;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * @author Administrator
 *
 */

class PersonBean implements Serializable {

	private static final long serialVersionUID = -1085021238019897841L;
	private Long aac001;
	private String aac003;
	private boolean aac004;
	private String aae135;
	private Object money;
	private List<Object> list;
	
	public List<Object> getList() {
		return list;
	}
	public void setList(List<Object> list) {
		this.list = list;
	}
	public Object getMoney() {
		return money;
	}
	public void setMoney(Object money) {
		this.money = money;
	}
	public Long getAac001() {
		return aac001;
	}
	public void setAac001(Long aac001) {
		this.aac001 = aac001;
	}
	public String getAac003() {
		return aac003;
	}
	public void setAac003(String aac003) {
		this.aac003 = aac003;
	}
	public boolean getAac004() {
		return aac004;
	}
	public void setAac004(boolean aac004) {
		this.aac004 = aac004;
	}
	public String getAae135() {
		return aae135;
	}
	public void setAae135(String aae135) {
		this.aae135 = aae135;
	}
	
}


public enum FastjsonSerial {
     INSTANCE;
	
	public void printHashMap() {
		Map<String,String> map = new HashMap<String,String>();
		map.put("a", null);
		map.put("b", "XXXXX");
		map.put("c", "");
		map.put("d", "①");
		map.put("e", "淘宝www.taobao.com");
		System.out.println("原始map:");
		System.out.println(map);
		System.out.println("map转json(同下):");
		System.out.println(JSON.toJSONString(map)); //没有将对应value为null的key输出
		System.out.println("map转json(格式化):");
		System.out.println(JSON.toJSONString(map,true));//没有将对应value为null的key输出
		
		System.out.println("map转json(保留null值):");
		System.out.println(JSON.toJSONString(map,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty));
		
		System.out.println("string还原map:");
		HashMap<String,String> json = JSON.parseObject(JSON.toJSONString(map,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty), 
				new TypeReference<HashMap<String,String>>(){});
		System.out.println(json);
	}
	
	
	public void printListHashMap() {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for(int i=0; i < 2; i++) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("a", null);
			map.put("b", "XXX");
			map.put("c", "");
			map.put("d", 12);
			map.put("e", true);
			list.add(map);
		}
		System.out.println("原始list:");
		System.out.println(list);
		System.out.println("listmap转json:");
		System.out.println(JSON.toJSONString(list)); //没有将对应value为null的key输出
		//System.out.println("listmap转json(格式化):");
		//System.out.println(JSON.toJSONString(list,true));//没有将对应value为null的key输出
	
		System.out.println("listmap转json(保留null值):");
		//SerializerFeature.WriteMapNullValue ---> 将对应value为null的key输出，而不是去掉
		String str = JSON.toJSONString(list,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty);
		System.out.println(str);
		
		System.out.println("listmap还原:");
		List<HashMap<String,Object>> json = JSON.parseObject(str, 
				new TypeReference<List<HashMap<String,Object>>>(){});
		System.out.println(json);
		
	}
	
	public void printBean() {
		PersonBean bean = new PersonBean();
		bean.setAac001(null);
		bean.setAac003("小明");
		//bean.setAac004(true);
		bean.setAae135(null);
		//bean.setMoney(null);
		
		System.out.println("Bean转json(非格式化):");
		//boolean不赋值默认为false
		//①SerializerFeature.WriteMapNullValue
		//若为bean时，该序列在里面讲String/object类型转为null,不加这个方法则在转为String时key将自动过滤
		//②SerializerFeature.WriteNullStringAsEmpty
		//将未赋值的String value或值为null的String value转为""(要配合①一起使用)
		//③SerializerFeature.WriteNullNumberAsZero
		//将bean中的number字段中的null转为0
		//④SerializerFeature.WriteNullListAsEmpty
		//将bean中的list值为null转为[]
		System.out.println(JSON.toJSONString(bean,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty));
		
	}
	
}
